﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using BkXitiTagger.Util;

namespace BkXitiTagger
{
    /// <summary>
    /// Extensions for strings
    /// </summary>
    public static class MiscExtensions
    {
        private const string UrlParamFormat = "&{0}={1}";

        /// <summary>
        /// You might prefer the usage of Uri.EscapeUriString for this purpose
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string UrlEncodeSpacePercentage(string str)
        {
            string encodedString = HttpUtility.UrlEncode(str);

            return encodedString.Replace("+", "%20");
        }

        /// <summary>
        /// Substitutes the pattern in the input string by the provided replacement
        /// </summary>
        /// <param name="value">The input string</param>
        /// <param name="pattern">The pattern to match and replace</param>
        /// <param name="replaceStr">The pattern to substitute</param>
        /// <returns>The modified string</returns>
        public static string ReplaceRegex(this string value, string pattern, string replaceStr)
        {
            if (string.IsNullOrEmpty(value))
            {
                return "";
            }
            if (string.IsNullOrEmpty(pattern))
            {
                throw new ArgumentNullException("pattern");
            }

            Regex regex = new Regex(pattern);
            return regex.Replace(value, replaceStr);
        }

        /// <summary>
        /// Converts a map of REST parameters to a string while escaping them
        /// </summary>
        /// <typeparam name="T">Type of dictionary key</typeparam>
        /// <typeparam name="U">Type of dictionary value</typeparam>
        /// <param name="dic">Map of URL parameters</param>
        /// <returns>Parameters as an URL-encoded string ready to be sent</returns>
        public static string ToUrlParametersEscaped<T, U>(this IDictionary<T, U> dic)
        {
            if (dic == null)
                return "";

            string stringUrlParams = "";
            foreach (KeyValuePair<T, U> param in dic)
            {
                stringUrlParams += string.Format(
                    MiscExtensions.UrlParamFormat,
                    MiscExtensions.UrlEncodeSpacePercentage(param.Key.ToString()),
                    MiscExtensions.UrlEncodeSpacePercentage(param.Value.ToString()));
            }

            return stringUrlParams;
        }

        /// <summary>
        /// Transform an enumeration into a printable string, based on the StringEnumValueAttribute attribute
        /// </summary>
        /// <typeparam name="T">The type of the enumeration to convert to string</typeparam>
        /// <param name="value">The instance of the enumeration value to convert</param>
        /// <returns>The string representation of the value</returns>
        public static string GetStringEnumValue<T>(this T value)
        {
            StringEnumValueAttribute[] attributes = (StringEnumValueAttribute[])value.GetType().GetField(value.ToString()).GetCustomAttributes(typeof(StringEnumValueAttribute), false);
            return attributes.Length > 0 ? attributes[0].StringValue : string.Empty;
        }
    }
}
